/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.json.action;

import com.json.bean.User;
import com.json.database.UserDao;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author hyberbin
 */
@WebServlet(name = "UpdateServlet", urlPatterns = {"/Update.jsp"})
public class UpdateServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("utf-8");
        String id = request.getParameter("id");
        UserDao userdao = new UserDao();
        int idint = 0;
        try {
            idint = Integer.parseInt(id);
        } catch (NumberFormatException numberFormatException) {
        }
        User user = userdao.getOneUser(idint);
        String send = "login.jsp";
        String notice = "";
        if (user.getUsername() != null) {//判空 
            //验证原来密码是否正确
            if (user.getPass().equals(request.getParameter("oldpass"))) {
                //判空 
                String pass = request.getParameter("pass");
                String repass = request.getParameter("repass");
                String username = request.getParameter("username");
                if (!username.equals("") && !pass.equals("") && !repass.equals("") && pass.equals(repass)) {
                  
                    user.setPass(pass);
                    user.setUsername(username);
                    boolean b = userdao.update(user);
                    if (b) {
                        notice = "更新成功！";
                    } else {
                        notice = "更新失败！";
                    }
                }
            } else {
                notice = "原来密码不正确！";
            }
        } else {
            notice = "用户不存在！";
        }
        request.setAttribute("notice", notice);
        request.getRequestDispatcher(send).forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
